package com.cl.service.payGrant;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.ObjectUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cl.dao.SearchDao;
import com.cl.entity.CcPay;
import com.cl.entity.UUser;
import com.cl.util.ClassNameReplaceUtil;

@Service
public class DataGenerator {

	@Autowired
	private SearchDao searchDao;

	@Value("#{applicationProperties['SECRETARY']}")
	private String secretary;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public HashMap generateGroupData(List<CcPay> list) throws ParseException {
		CcPaySumImpl cpSi = new CcPaySumImpl();
		HashMap hp = new HashMap();
		List gList = new ArrayList();
		List jList = new ArrayList();
		List wList = new ArrayList();
		List oList = new ArrayList();
		List jlList = new ArrayList();
		List jrList = new ArrayList();
		
		for (int i = 0; i < list.size(); i++) {
			com.cl.entity.CcPay cp = list.get(i);
			cpSi.putCcPayByAuthorId(cp);
		}
		//j 人员信息+金额
		HashMap hm = cpSi.getHm();
		int index = 0;
		Integer[] inx = new Integer[] { 3, 3, 3 };
		for (Object obj : hm.keySet()) {
			//j key人员信息
			String key = ObjectUtils.toString(obj);
			//c4w 
			/*
			if(key==null || hm.get(key)==null){
				continue;
			}
			*/
			HashMap keyMapper = parserAuthorName(key);
			// HashMap hashmap1 =new HashMap();
//			String keyMapper1 = PayGrantExcelService.getHttp();
			//hashmap1.put("keyMapper1",keyMapper1);
			/*
			Object author = keyMapper.get("author");
			Object wn = keyMapper.get("wn");
			
			if(author==null || wn==null)
				continue;
			*/
//			PayGrantExcelService c = new PayGrantExcelService(); 
//			String resultStr;
//	        resultStr=c.httpPost(); 
			switch (CharUtils.toChar(ObjectUtils.toString(keyMapper.get("type")))) {
			case 'G':
				gList.add(new CcPayExcel(keyMapper.get("author").toString(), "", "g", keyMapper.get("org").toString(),
						Integer.parseInt(hm.get(key).toString())));

				break;
			case 'J':
//				System.out.println("oooooooooo"+hm.get(key));
				//j 增加null判断
				if(hm.get(key)!=null){
				jList.add(new CcPayExcel(keyMapper.get("author").toString(), "", "j", "",
						Integer.parseInt(hm.get(key).toString())));
				inx[1]++;
				}else{
					continue;
				}
				break;
			case 'W':
				wList.add(new CcPayExcel(keyMapper.get("author").toString(), keyMapper.get("wn")==null?"":keyMapper.get("wn").toString(), "w", "",
								Integer.parseInt(hm.get(key).toString())));
				break;
				//j 添加O type
			case 'O':
				oList.add(new CcPayExcel(keyMapper.get("author").toString(), "", "o","",
						(int)Double.parseDouble(hm.get(key).toString())));
	//			inx[1]++;
				break;
			
			default:
				break;
			}
			index++;
		}
		if (jList.size() > 0) {
			for (int i = 0; i < jList.size(); i++) {
				CcPayExcel c = (CcPayExcel) jList.get(i);
				if (c.getName().trim().indexOf("资料信息")>=0) {
					hp.put("zx", c.getPayResult());
					List orgUser = searchDao.listOrgUsers("ou=ziliaoxinxi", 1000);
					if (orgUser.size() > 0) {
						BigDecimal bd = BigDecimal.valueOf(c.getPayResult());
						BigDecimal pPay = bd.divide(BigDecimal.valueOf(orgUser.size()), 0, BigDecimal.ROUND_DOWN);
						Iterator it = orgUser.iterator();
						int idex = 1;
						CcPayExcel se = new CcPayExcel();
						while (it.hasNext()) {
							UUser u = (UUser) it.next();
							CcPayExcel cpe = new CcPayExcel();
						// j 稿酬平均	
							cpe.setName(u.getName());
							cpe.setPayResult(pPay.doubleValue());
							cpe.setIndex(idex);
							jlList.add(cpe);
							idex++;
//							if (!u.getUserId().equals(secretary)) {
//								cpe.setName(u.getName());
//								cpe.setPayResult(pPay.doubleValue());
//								cpe.setIndex(idex);
//								jlList.add(cpe);
//								idex++;
//							} else {
//								double p = bd.subtract(pPay.multiply(BigDecimal.valueOf(orgUser.size() - 1)))
//										.doubleValue();
//								se.setName(u.getName());
//								se.setPayResult(p);
//								se.setIndex(orgUser.size());
//							}
						}
						jlList.add(se);
					}
				}
			}
			int j = Math.round(jlList.size() / 2);
			for (int i = 0; i < Math.round(jlList.size() / 2); i++) {
				CcPayExcel[] cp = new CcPayExcel[2];
				cp[0] = (CcPayExcel) jlList.get(i);
				if (j < jlList.size()) {
					cp[1] = (CcPayExcel) jlList.get(j);
				}
				jrList.add(cp);
				j++;
			}
		}

//	if (oList.size() > 0) {
//		Iterator it = oList.iterator();
//		for (int i = 0; i < oList.size(); i++) {
//			CcPayExcel c = (CcPayExcel) oList.get(i);
//			if (c.getName()!=null) {
////				Iterator it = oList.iterator();
//				int index1 = 1;
//				while (it.hasNext()) {
//					CcPayExcel cpe1 = new CcPayExcel();
//					cpe1.setIndex(index1);
//					oList.add(cpe1);
//					index1++;	
//				}
//			}
//		}
//	}
		hp.put("g", gList);
		hp.put("o", oList);
		hp.put("j", jList);
		hp.put("w", wList);
		hp.put("jr", jrList);
		return hp;

	}

	public static List generatePersonDetailData(List<CcPay> list) throws ParseException {
		for (int i = 0; i < list.size(); i++) {
			CcPay c = list.get(i);
			if (c.getAuthorId() != null && c.getAuthorId().trim().equals("") == false) {
				HashMap keyMapper = parserAuthorName(c.getAuthorId());
				c.setAuthorId(keyMapper.get("author").toString());
			}
			if (c.getPayFactorNotes() != null && c.getPayFactorNotes().trim().equals("") == false) {
				c.setPayFactorNotes(GcChatUtils.parseStringZkh(c.getPayFactorNotes()));
			}
			if(c.getSubtype()==null || c.getSubtype().trim().equals("")){
				c.setSubtype(ClassNameReplaceUtil.replaceName(c.getClass_()));
			}
			
		}

		return list;
	}

	private static HashMap parserAuthorName(String key) throws ParseException {
		HashMap keyMapper = GcChatUtils.parseAuthorName(key);
		return keyMapper;
	}

}